package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysScopeDepart;
import org.jeecg.modules.system.mapper.SysScopeDepartMapper;
import org.jeecg.modules.system.service.ISysScopeDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 作用域授权
 * @Author: jeecg-boot
 * @Date:   2019-10-05
 * @Version: V1.0
 */
@Service
public class SysScopeDepartServiceImpl extends ServiceImpl<SysScopeDepartMapper, SysScopeDepart> implements ISysScopeDepartService {

    @Autowired
    SysScopeDepartMapper sysScopeDepartMapper;

    @Override
    @CacheEvict(value= CacheConstant.SCOPE_DEPART_RULES_CACHE, allEntries=true)
    public void saveScopeDepart(String scopeId, String departIds, String lastDepartIds,int halfStatus) {
        List<String> add = getDiff(lastDepartIds,departIds);
        if(add!=null && add.size()>0) {
            List<SysScopeDepart> list = new ArrayList<SysScopeDepart>();
            for (String p : add) {
                if(oConvertUtils.isNotEmpty(p)) {
                    SysScopeDepart scopeAdd = new SysScopeDepart(scopeId, p,halfStatus);
                    list.add(scopeAdd);
                }
            }
            this.saveBatch(list);
        }
        List<String> delete = getDiff(departIds,lastDepartIds);
        if(delete!=null && delete.size()>0) {
            for (String departId : delete) {
                this.remove(new QueryWrapper<SysScopeDepart>().lambda().eq(SysScopeDepart::getScopeId, scopeId).eq(SysScopeDepart::getDepartId, departId));
            }
        }
    }

    @Override
    @CacheEvict(value= CacheConstant.SCOPE_DEPART_RULES_CACHE, allEntries=true)
    public void saveServerScopeDepart(String tenantId, String scopeId, String departIds, String lastDepartIds, int halfStatus) {
        List<String> add = getDiff(lastDepartIds,departIds);
        if(add!=null && add.size()>0) {
            List<SysScopeDepart> list = new ArrayList<SysScopeDepart>();
            for (String p : add) {
                if(oConvertUtils.isNotEmpty(p)) {
                    SysScopeDepart scopeAdd = new SysScopeDepart(tenantId,scopeId, p,halfStatus);
                    list.add(scopeAdd);
                }
            }
            sysScopeDepartMapper.saveServerBatch(list);
        }
        List<String> delDepartIds = getDiff(departIds,lastDepartIds);
        if(delDepartIds != null && delDepartIds.size()>0) {
            sysScopeDepartMapper.delServerBatch(tenantId,scopeId,delDepartIds);
        }
    }

    @Override
    public void serverSave(SysScopeDepart sysScopeDepart) {
        sysScopeDepartMapper.serverSave(sysScopeDepart);
    }

    @Override
    public List<SysScopeDepart> queryServerScopeDepart(String scopeId, String tenantId) {
        return sysScopeDepartMapper.queryServerScopeDepart(scopeId,tenantId);
    }



    /**
     * 从diff中找出main中没有的元素
     * @param main
     * @param diff
     * @return
     */
    private List<String> getDiff(String main, String diff){
        if(oConvertUtils.isEmpty(diff)) {
            return null;
        }
        if(oConvertUtils.isEmpty(main)) {
            return Arrays.asList(diff.split(","));
        }
        String[] mainArr = main.split(",");
        String[] diffArr = diff.split(",");
        Map<String, Integer> map = new HashMap<>();
        for (String string : mainArr) {
            map.put(string, 1);
        }
        List<String> res = new ArrayList<String>();
        for (String key : diffArr) {
            if(oConvertUtils.isNotEmpty(key) && !map.containsKey(key)) {
                res.add(key);
            }
        }
        return res;
    }
}
